package com.szewec.ps.ui.road.facilities.investigation.task.roadside.facilities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.szewec.ps.R;
import com.szewec.ps.base.Constants;
import com.szewec.ps.base.Enums;
import com.szewec.ps.databinding.FragmentRoadTollGateBinding;
import com.szewec.ps.model.road.facilities.investigation.road.side.facilities.RoadTollGate;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTask;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTaskDTO;
import com.szewec.ps.model.selectmappoint.SelectedPoint;
import com.szewec.ps.ui.road.facilities.intersection.task.views.RadioBoxView;
import com.szewec.ps.util.RollUtil;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.TextViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 收费站
 */
public class RoadTollGateFragment extends Fragment {
    private FragmentRoadTollGateBinding binding;

    // 路段设施调查任务DTO
    private RoadFacilitiesInvestigationTaskDTO.RoadFacilitiesTaskDTO roadFacilitiesTaskDTO;
    private RoadTollGate roadTollGate;

    // 建造位置 数据字典
    private List<String> constructionLocationDict = new ArrayList<>();
    // 车道数 数据字典
    private List<String> laneTotalDict = new ArrayList<>();
    // 坐标
    private String selectedPoint;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_road_toll_gate, container, false);
        // 2、获取到视图
        View inflate = binding.getRoot();
        ButterKnife.bind(this, inflate);

        initData();
        initView();
        return inflate;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        // 通过Activity中的public方法来获取值
        roadFacilitiesTaskDTO = ((RoadsideFacilitiesActivity) context).pushFragmentData();
    }

    private void initData() {
        constructionLocationDict = Enums.TollGateConstructionLocationEnum.getEnumValueList();
        laneTotalDict = Enums.TollGateLaneTotalEnum.getEnumValueList();
        roadTollGate = roadFacilitiesTaskDTO.getRoadTollGate() == null ? new RoadTollGate() : roadFacilitiesTaskDTO.getRoadTollGate();
    }

    private void initView() {
        // 获取坐标
        SelectedPoint selectedPoint = JSONObject.parseObject(roadTollGate.getCoordinate(), SelectedPoint.class);
        selectedPoint = selectedPoint == null ? new SelectedPoint() : selectedPoint;

        binding.roadName.setValue(roadTollGate.getRoadName());
        binding.coordinate.setValue(selectedPoint.getxCoordinate(), selectedPoint.getyCoordinate());
        binding.constructionLocation.setValue(Enums.TollGateConstructionLocationEnum.getValueByKey(roadTollGate.getConstructionLocation()));
        // 车道数
        Integer laneTotal = roadTollGate.getLaneTotal();
        binding.laneTotal.setValue(Enums.TollGateLaneTotalEnum.getValueByKey(laneTotal));
        // 收费站进高速方向 ETC专用车道
        Integer etcLaneIn = roadTollGate.getEtcLaneIn() == null ? RadioBoxView.ONE : roadTollGate.getEtcLaneIn();
        // 收费站进高速方向 人工收费通道
        Integer artificialLaneIn = roadTollGate.getArtificialLaneIn() == null ? RadioBoxView.ONE : roadTollGate.getArtificialLaneIn();
        // 收费站进高速方向 有无绿色通道
        Integer greenChannelIn = roadTollGate.getGreenChannelIn() == null ? RadioBoxView.ONE : roadTollGate.getGreenChannelIn();
        // 收费站出高速方向 ETC专用车道
        Integer etcLaneOut = roadTollGate.getEtcLaneOut() == null ? RadioBoxView.ONE : roadTollGate.getEtcLaneOut();
        // 收费站出高速方向 人工收费通道
        Integer artificialLaneOut = roadTollGate.getArtificialLaneOut() == null ? RadioBoxView.ONE : roadTollGate.getArtificialLaneOut();
        // 收费站出高速方向 有无绿色通道
        Integer greenChannelOut = roadTollGate.getGreenChannelOut() == null ? RadioBoxView.ONE : roadTollGate.getGreenChannelOut();

        // 根据车道数展示对应车道数据
        if (laneTotal != null) {
            if (Enums.TollGateLaneTotalEnum.HIGH_SPEED_IN.getKey().equals(laneTotal)) {
                binding.etcLaneIn.setChecked(etcLaneIn);
                binding.artificialLaneIn.setChecked(artificialLaneIn);
                binding.greenChannelIn.setChecked(greenChannelIn);
                binding.llContainerIn.setVisibility(View.VISIBLE);
            } else if (Enums.TollGateLaneTotalEnum.HIGH_SPEED_OUT.getKey().equals(laneTotal)) {
                binding.etcLaneOut.setChecked(etcLaneOut);
                binding.artificialLaneOut.setChecked(artificialLaneOut);
                binding.greenChannelOut.setChecked(greenChannelOut);
                binding.llContainerOut.setVisibility(View.VISIBLE);
            }
        }
    }

    public RoadTollGate getData() {
        // 添加路段调查任务ID
        RoadFacilitiesInvestigationTask facilitiesInvestigationTask = roadFacilitiesTaskDTO.getFacilitiesInvestigationTask();
        if (facilitiesInvestigationTask != null && StringUtils.isNotEmpty(facilitiesInvestigationTask.getId())) {
            roadTollGate.setRoadInvestigationTaskId(facilitiesInvestigationTask.getId());
        }
        roadTollGate.setRoadName(binding.roadName.getStringValue());
        roadTollGate.setCoordinate(selectedPoint);
        roadTollGate.setConstructionLocation(Enums.TollGateConstructionLocationEnum.getKeyByValue(binding.constructionLocation.getStringValue()));
        // 车道数
        Integer laneTotal = Enums.TollGateLaneTotalEnum.getKeyByValue(binding.laneTotal.getStringValue());
        roadTollGate.setLaneTotal(laneTotal);
        // 根据车道数保存对应的车道数据 并将另一选项的值清空
        if (laneTotal != null) {
            if (Enums.TollGateLaneTotalEnum.HIGH_SPEED_IN.getKey().equals(laneTotal)) {
                roadTollGate.setEtcLaneIn(binding.etcLaneIn.getChecked());
                roadTollGate.setArtificialLaneIn(binding.artificialLaneIn.getChecked());
                roadTollGate.setGreenChannelIn(binding.greenChannelIn.getChecked());
                roadTollGate.setEtcLaneOut(null);
                roadTollGate.setArtificialLaneOut(null);
                roadTollGate.setGreenChannelOut(null);
            } else if (Enums.TollGateLaneTotalEnum.HIGH_SPEED_OUT.getKey().equals(laneTotal)) {
                roadTollGate.setEtcLaneOut(binding.etcLaneOut.getChecked());
                roadTollGate.setArtificialLaneOut(binding.artificialLaneOut.getChecked());
                roadTollGate.setGreenChannelOut(binding.greenChannelOut.getChecked());
                roadTollGate.setEtcLaneIn(null);
                roadTollGate.setArtificialLaneIn(null);
                roadTollGate.setGreenChannelIn(null);
            }
        }
        return roadTollGate;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        SelectedPoint centerPoint = null;
        //选择了坐标
        if (resultCode == Constants.RESULT_CODE.SELECTED_POINT) {
            if (data != null) {
                Bundle presentValue = data.getExtras();
                centerPoint = (SelectedPoint) presentValue.getSerializable(Constants.EXTRA_KEY.MAP_CENTER_POINT);
            }
        }
        if (centerPoint == null) {
            return;
        }
        selectedPoint = JSONObject.toJSONString(centerPoint);
    }

    @OnClick({R.id.constructionLocation, R.id.laneTotal})
    public void onViewClicked(View view) {
        //隐藏系统软键盘
        TextViewUtil.closeKeyboard(getActivity());
        switch (view.getId()) {
            case R.id.constructionLocation:
                RollUtil.alertBottomWheelOption(getContext(), constructionLocationDict, binding.constructionLocation.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.constructionLocation.setValue(constructionLocationDict.get(index));
                    }
                });
                break;
            case R.id.laneTotal:
                RollUtil.alertBottomWheelOption(getContext(), laneTotalDict, binding.laneTotal.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.laneTotal.setValue(laneTotalDict.get(index));
                        // 如果选择进高速方向 展示收费站进高速方向模块，隐藏收费站出高速方向模块
                        if (Enums.TollGateLaneTotalEnum.HIGH_SPEED_IN.getValue().equals(laneTotalDict.get(index))) {
                            binding.llContainerIn.setVisibility(View.VISIBLE);
                            binding.llContainerOut.setVisibility(View.GONE);
                        } else {
                            binding.llContainerIn.setVisibility(View.GONE);
                            binding.llContainerOut.setVisibility(View.VISIBLE);
                        }
                    }
                });
        }
    }
}